package com.kun.action;


import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.kun.entity.CustomerEntity;
import com.kun.server.LoginServer;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Namespace("/")
public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private CustomerEntity customerEntity;
	
	@Resource(name="LoginServerImpl")
	private LoginServer ls;
	
	/**
	 * 登录的方法
	 * @return
	 */
	@Action(value="login",results={
			@Result(name="success",location="/index.jsp"),
			@Result(name="error",location="/index.jsp")
	})
	public String login(){
		List<CustomerEntity> list = ls.login(customerEntity);
		if(list.size() > 0){
			ServletActionContext.getRequest().getSession().setAttribute("customerid",list.get(0).getCustomerid());
			ServletActionContext.getRequest().getSession().setAttribute("customerName",list.get(0).getCustomerName());
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	
	/**
	 * 退出登录 清除全部session
	 * @return
	 */
	@Action(value="top_exit",results={
			@Result(name="success",location="/index.jsp"),
	})
	public String exitLogin(){
		ServletActionContext.getRequest().getSession().invalidate();
		return SUCCESS;
	}


	public CustomerEntity getCustomerEntity() {
		return customerEntity;
	}
	public void setCustomerEntity(CustomerEntity customerEntity) {
		this.customerEntity = customerEntity;
	}
	
	
	
	
}
